$padding-basis: 0.75em;

@media screen and (width >= 925px) {
  .container.posts {
    // needs to be static, otherwise we get content shifts when the TOC shows/hides
    grid-template-columns: 75% 25%;
  }
}

.d-toc-main {
  min-width: 6em;
  max-width: 13em;
  word-wrap: break-word;
  box-sizing: border-box;

  .overlay & {
    max-width: 100%;
  }

  a {
    display: block;
    padding: 0.15em 0;
    color: var(--primary-medium);

    &.scroll-to-bottom {
      padding-left: $padding-basis;
    }
  }

  .timeline-toggle {
    margin-top: 1em;
  }

  #d-toc {
    border-left: 1px solid var(--primary-low);
    max-height: calc(100vh - 4.5em - var(--header-offset));
    overflow: auto;

    ul {
      list-style-type: none;
      margin: 0;
      padding: 0;
    }

    li.d-toc-item {
      margin: 0;
      padding: 0;
      padding-left: $padding-basis;
      line-height: var(--line-height-large);

      > ul {
        max-height: 0;
        overflow: hidden;
        opacity: 0.5;
        transition:
          opacity 0.3s ease-in-out,
          max-height 0.3s ease-in-out;
      }

      &.active,
      .d-toc-wrapper.overlay & {
        ul {
          max-height: 500em;
          overflow: visible;
          opacity: 1;
          animation: hide-scroll 0.3s backwards;
        }

        // hides the scrollbar while subsection expands
        @keyframes hide-scroll {
          from,
          to {
            overflow: hidden;
          }
        }
      }

      > a:hover {
        color: var(--primary-high);
      }

      &.direct-active > a {
        position: relative;
        color: var(--primary);

        &::before {
          content: "";
          width: 1px;
          margin-top: -1px;
          background-color: var(--tertiary);
          position: absolute;
          height: 100%;
        }
      }
    }

    > ul > li > ul {
      font-size: var(--font-down-1);

      > li:first-child {
        padding-top: 0.25em;
      }

      > li {
        padding-bottom: 0.15em;
      }

      li.direct-active > a::before {
        // it's odd that we need this
        margin-left: -1px;
      }

      li.d-toc-h2 ~ li.d-toc-h3,
      li.d-toc-h2 ~ li.d-toc-h4,
      li.d-toc-h2 ~ li.d-toc-h5,
      li.d-toc-h3 ~ li.d-toc-h4,
      li.d-toc-h3 ~ li.d-toc-h5,
      li.d-toc-h4 ~ li.d-toc-h5 {
        > a {
          padding-left: $padding-basis;
        }
      }
    }
  }
}

// active line marker
$selector: "> ul > li.direct-active > a:before";
$map: (
  "left": "html:not(.rtl)",
  "right": "html.rtl",
);

/*
// loop below generates styling for non-RTL and RTL
// Example:
html:not(.rtl) SELECTOR {
    left: -.75em
}
html.rtl SELECTOR {
    right: -.75em
}
*/
@each $prop, $parent in $map {
  #{$parent} #d-toc {
    #{$selector} {
      #{$prop}: (-$padding-basis);
    }
    > ul > li #{$selector} {
      #{$prop}: (-$padding-basis) * 2;
    }
  }
}

// END active line marker
.d-toc-mini {
  height: 100%;

  button {
    height: 100%;
  }
}

// overlaid timeline (on mobile and narrow screens)
.topic-navigation.with-topic-progress {
  .d-toc-wrapper {
    position: fixed;
    margin-top: 0.25em;
    height: calc(100vh - 50px - var(--header-offset));
    opacity: 0.5;
    right: -100vw;
    top: var(--header-offset);
    width: 75vw;
    max-width: 350px;
    background-color: var(--secondary);
    box-shadow: var(--shadow-dropdown);
    z-index: z("modal", "overlay");
    transition: all 0.2s ease-in-out;

    .d-toc-main {
      width: 100%;
      padding: 0.5em;
      height: 100%;

      #d-toc {
        max-height: calc(100% - 3em);
      }
    }

    &.overlay {
      right: 0;
      width: 75vw;
      opacity: 1;

      .d-toc-main #d-toc li.d-toc-item ul {
        transition: none;
      }
    }

    a.scroll-to-bottom,
    a.d-toc-close {
      display: inline-block;
      padding: 0.5em;
    }

    .d-toc-icons {
      text-align: right;
    }
  }
}

// core sets first child's top margin to 0
// ensure it's also 0 when TOC markup is first
.cooked > div[data-theme-toc]:first-child + * {
  margin-top: 0;
}

// RTL Support
.rtl {
  .d-toc-main {
    border-left: none;
    border-right: 1px solid var(--primary-low);

    #d-toc li.d-toc-item,
    a.scroll-to-bottom {
      padding-left: 0;
      padding-right: $padding-basis;
    }
  }

  .topic-navigation.with-topic-progress .d-toc-wrapper {
    right: unset;
    left: -100vw;

    &.overlay {
      right: unset;
      left: 0;
    }
  }
}

// Composer preview notice
.edit-title .d-editor-preview [data-theme-toc],
body.toc-for-replies-enabled .d-editor-preview [data-theme-toc] {
  background: var(--tertiary);
  color: var(--secondary);
  position: sticky;
  z-index: 1;
  top: 0;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;

  &::before {
    content: "#{$composer_toc_text}";
  }
}

// Docs plugin outlet
.below-docs-topic-outlet.d-toc-wrapper {
  position: sticky;
  top: calc(var(--header-offset, 60px) + 1em);
  max-height: calc(100vh - 2em - var(--header-offset, 60px));

  .mobile-view & {
    display: none;
  }

  .d-toc-main {
    display: block;
  }
}

// toggle in timeline
.timeline-container
  .topic-timeline
  .timeline-footer-controls
  button:last-child {
  // annoying core style
  &.timeline-toggle {
    margin-right: 100%;
    white-space: nowrap;
  }
}

// jump to bottom in timeline
.d-toc-footer-icons {
  font-size: var(--font-down-1);
  margin-top: 0.5em;

  button {
    color: var(--tertiary);

    .d-icon {
      color: currentcolor;
    }
  }
}

// on shorter screens, we can keep this consistently in the same location
// this is kind of far away for tall screens, so the more variable position below might be better
@media screen and (height <= 950px) {
  .timeline-toggle {
    position: fixed;
    bottom: 0;
  }
}

// hides the timeline when d-toc is shown
.d-toc-active {
  .timeline-container {
    display: none;
  }
}

// hide the toggle in the expanded timeline on mobile
.timeline-fullscreen {
  .timeline-toggle {
    display: none;
  }
}
